Skip to content

fix(query-insights): traverse shard branches in planner-tree helpers#703

Open
hanhan761 wants to merge 2 commits into
microsoft:mainfrom
hanhan761:fix-620-shard-traversal
Open

fix(query-insights): traverse shard branches in planner-tree helpers#703
hanhan761 wants to merge 2 commits into
microsoft:mainfrom
hanhan761:fix-620-shard-traversal

Conversation

@hanhan761

Copy link
Copy Markdown
Contributor

Problem

findStageInPlan() and collectPlannerStages() only traversed inputStage and inputStages — they missed stages nested under shards structures in sharded cluster explain plans. This meant planner-stage signals like isBitmap (bitmap index detection) could be silently missed for sharded DocumentDB clusters.

In contrast, traverseStages() (execution-stage traversal for UI) already handled shards.

Fix

  • findStageInPlan() (ExplainPlanAnalyzer.ts): added shards array recursion, matching the pattern used by traverseStage() and checkStageForSort() elsewhere in the same file.
  • collectPlannerStages() (StagePropertyExtractor.ts): added shards array recursion, matching traverseStages() in the same class.

Tests

Two new test cases added:

  1. IXSCAN with isBitmap under shard pathSHARD_MERGE → shards → SINGLE_SHARD → FETCH → IXSCAN
  2. Mixed planner/exec sharding — planner is sharded but execution stats are flat

All 15 existing + 2 new tests pass. ✅

Closes #620

🤖 Generated with Claude Code

@
fix(query-insights): traverse shard branches in planner-tree helpers

`findStageInPlan()` and `collectPlannerStages()` only traversed
inputStage and inputStages — they missed stages nested under
shards structures in sharded cluster explain plans. This meant
planner-stage signals like isBitmap (bitmap index) and
low-cardinality index detection could be silently missed.

- Extend findStageInPlan() to recurse through shards arrays
  (matching traverseStage / checkStageForSort behavior).
- Extend collectPlannerStages() similarly.
- Add two sharded test cases: IXSCAN with isBitmap under a
  SHARD_MERGE > shards > SINGLE_SHARD path, and a mixed-case
  where planner is sharded but exec stats are flat.

Closes microsoft#620
@
Copilot AI review requested due to automatic review settings May 29, 2026 13:45
@hanhan761 hanhan761 requested a review from a team as a code owner May 29, 2026 13:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds shard-branch traversal support to query insight analysis so stage detection works correctly for sharded explain plans.

Changes:

  • Recurse into shards branches when flattening planner stages.
  • Recurse into shards branches when searching for a stage in a plan tree.
  • Add tests covering shard-branch scenarios for bitmap index detection.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/documentdb/queryInsights/StagePropertyExtractor.ts Extends planner-stage collection logic to traverse sharded branches.
src/documentdb/queryInsights/ExplainPlanAnalyzer.ts Extends stage search to recurse into shard branches.
src/documentdb/queryInsights/ExplainPlanAnalyzer.addIndexStrategyAdvisories.test.ts Adds regression tests for shard-branch bitmap stage detection.

Comment on lines +78 to +82
if (stage.shards && Array.isArray(stage.shards)) {
for (const shard of stage.shards) {
this.collectPlannerStages(shard as Document, accumulator);
}
}
Comment on lines +774 to +781
if (plan.shards && Array.isArray(plan.shards)) {
for (const shard of plan.shards) {
const found = this.findStageInPlan(shard as Document, stageName);
if (found) {
return found;
}
}
}
});

it('detects IXSCAN in planner shards even when exec stats use flat structure', () => {
// Edge case: planner is sharded, execution stats may not be
@tnaum-ms tnaum-ms added the in-triage-queue We've seen your input and will triage it label Jun 4, 2026
@tnaum-ms tnaum-ms added on-hold Approved but not merging yet. Reason in comments. Remove + click Ready for review to release. and removed in-triage-queue We've seen your input and will triage it labels Jun 19, 2026
@tnaum-ms tnaum-ms added this to the 0.10.1 milestone Jun 19, 2026
@tnaum-ms

Copy link
Copy Markdown
Collaborator

Thanks for this one. The shard branch traversal is a real gap and the direction here is right.

We are putting this on hold and scheduling it for the 0.10.1 release. The reason is testing. Doing this correctly means handling the different shapes of sharded explain output across platforms, and the current tests use simplified fixtures that do not match real sharded explain output, so we cannot validate it with confidence yet. We want a larger test setup before we commit to the change.

We expect to have that after our MongoDB Atlas discovery work ships, which will let us test against a wider range of platforms. We have marked the PR as on hold and moved it to the 0.10.1 milestone, and we will pick it back up then. Appreciate your patience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

on-hold Approved but not merging yet. Reason in comments. Remove + click Ready for review to release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Query Insights: Planner-tree traversal helpers miss shard branches

3 participants